home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / term-source.lha / Crypt.c < prev    next >
C/C++ Source or Header  |  1996-10-20  |  3KB  |  131 lines

  1. /*
  2. **    Crypt.c
  3. **
  4. **    Data encryption routines.
  5. **
  6. **    Copyright © 1990-1996 by Olaf `Olsen' Barthel
  7. **        All Rights Reserved
  8. */
  9.  
  10. #ifndef _GLOBAL_H
  11. #include "Global.h"
  12. #endif
  13.  
  14.     /* The width of the cell ring is defined here. A word of warning: never
  15.      * encrypt a file and forget the access password, since it will be
  16.      * extremely hard to break the code (with 30 cells there are 256^30
  17.      * possible values each cell may be initialized to which means that
  18.      * you could have to try more than 1.7e+72 combinations for each single
  19.      * password character before actually finding the matching password).
  20.      * See `Random sequence generation by cellular automata' by Steven
  21.      * Wolfram (Institute for Advanced Study; Advances in Applied
  22.      * Mathematics) for more information.
  23.      */
  24.  
  25. #define CELL_WIDTH 32
  26.  
  27.     /* The cell ring and the ring index pointers. */
  28.  
  29. STATIC UBYTE Cell[2][CELL_WIDTH],*Src,*Dst;
  30.  
  31.     /* The pattern the cell ring will be filled with. The pattern
  32.      * may seem familiar: these are the first 32 digits of Pi.
  33.      */
  34.  
  35. STATIC UBYTE Pi[32] = { 3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,8,4,6,2,6,4,3,3,8,3,2,7,9,5 };
  36.  
  37.     /* Automaton():
  38.      *
  39.      *    A cellular automaton working on a ring of cells, producing
  40.      *    random data in each single cell.
  41.      */
  42.  
  43. STATIC LONG
  44. Automaton(VOID)
  45. {
  46.     UBYTE *A,*B,*Swap;
  47.     LONG i;
  48.  
  49.     A = Src;
  50.     B = Dst;
  51.  
  52.         /* Operate on the cell ring... */
  53.  
  54.     for(i = 1 ; i < CELL_WIDTH - 1 ; i++)
  55.         B[i] = A[i - 1] ^ (A[i] | A[i + 1]);
  56.  
  57.         /* Operate on first and last element. */
  58.  
  59.     B[0]            = A[CELL_WIDTH - 1] ^ (A[0]              | A[1]);
  60.     B[CELL_WIDTH - 1]    = A[CELL_WIDTH - 2] ^ (A[CELL_WIDTH - 1] | A[0]);
  61.  
  62.         /* Swap cell rings. */
  63.  
  64.     Swap    = Src;
  65.     Src    = Dst;
  66.     Dst    = Swap;
  67.  
  68.         /* Return contents of first cell. */
  69.  
  70.     return(A[0]);
  71. }
  72.  
  73.     /* Encrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  74.      *
  75.      *    Encrypt data using cellular automaton as a random number generator.
  76.      */
  77.  
  78. VOID
  79. Encrypt(UBYTE *Source,LONG SourceLen,UBYTE *Destination,UBYTE *Key,LONG KeyLen)
  80. {
  81.     LONG i;
  82.  
  83.         /* Set up cell ring index pointers. */
  84.  
  85.     Src = Cell[0];
  86.     Dst = Cell[1];
  87.  
  88.     if(KeyLen < 32)
  89.         CopyMem(Pi,&Src[KeyLen],32 - KeyLen);
  90.  
  91.     CopyMem(Key,Src,KeyLen);
  92.  
  93.         /* Encrypt the source data. */
  94.  
  95.     for(i = 0 ; i < SourceLen ; i++)
  96.         *Destination++ = ((LONG)Source[i] + Automaton()) % 256;
  97. }
  98.  
  99.     /* Decrypt(UBYTE *Source,UBYTE *Destination,UBYTE *Key):
  100.      *
  101.      *    Decrypt data using cellular automaton as a random number generator.
  102.      */
  103.  
  104. VOID
  105. Decrypt(UBYTE *Source,LONG SourceLen,UBYTE *Destination,UBYTE *Key,LONG KeyLen)
  106. {
  107.     LONG i,Code;
  108.  
  109.         /* Set up cell ring index pointers. */
  110.  
  111.     Src = Cell[0];
  112.     Dst = Cell[1];
  113.  
  114.     if(KeyLen < 32)
  115.         CopyMem(Pi,&Src[KeyLen],32 - KeyLen);
  116.  
  117.     CopyMem(Key,Src,KeyLen);
  118.  
  119.         /* Decrypt the source data. */
  120.  
  121.     for(i = 0 ; i < SourceLen ; i++)
  122.     {
  123.         Code = ((LONG)Source[i]) - Automaton();
  124.  
  125.         if(Code < 0)
  126.             Code += 256;
  127.  
  128.         *Destination++ = Code;
  129.     }
  130. }
  131.